package com.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.admin.entity.Component;
import com.admin.entity.Position;
import com.admin.mapper.ComponentMapper;
import com.admin.mapper.ContentMapper;
import com.admin.mapper.FileinfoMapper;
import com.admin.mapper.PositionMapper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bean.ComponentVO;
import com.bean.PositionVO;
import com.admin.entity.Fileinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 此类主要用于处理编辑电视屏幕业务
 *
 * @author wangfei
 */
@SuppressWarnings("ALL")
@Service
public class DragService {
    //组件表
    @Autowired
    private ComponentMapper component;
    //组件位置表
    @Autowired
    private PositionMapper position;
    //屏幕内容表
    @Autowired
    private ContentMapper content;
    //图片表
    @Autowired
    private FileinfoMapper pictrul;

    @Value("${upload.dir}")
    private String upload;

    //查询各个组件位置表中全部数据
    public List<PositionVO> getPositions() {
        List<Position> list = position.selectList(null);
        List<PositionVO> newList = new ArrayList<PositionVO>();
        for (Position p : list) {
            PositionVO pos = new PositionVO();
            BeanUtil.copyProperties(p, pos, "pos");
            pos.setPosition(JSONObject.parseObject(p.getPos(), Map.class));
            newList.add(pos);
        }
        return newList;
    }

    //查询出所有组件
    public List<ComponentVO> getAllComponents() {
        QueryWrapper<Component> query = new QueryWrapper<Component>();
        query.select("id", "name", "types", "code", "img", "width", "height");
        query.eq("status", 1);
        List<Component> list = component.selectList(query);
        List<ComponentVO> newList = new ArrayList<ComponentVO>();
        for (Component p : list) {
            ComponentVO pos = new ComponentVO();
            BeanUtil.copyProperties(p, pos, "types");
            pos.setPositions(JSONArray.parseObject(p.getTypes(), List.class));
            newList.add(pos);
        }
        return newList;
    }

    //将图片写入数据库
    public Fileinfo addImg(Fileinfo p) {
        pictrul.insert(p);
        return p;
    }

    //根据md5值查询图片
    public boolean isExist(String md5) {
        QueryWrapper<Fileinfo> query = new QueryWrapper<Fileinfo>();
        query.eq(true, "md5", md5);
        Fileinfo p = pictrul.selectOne(query);
        if (p != null) {

            String url = p.getUrl();
            File file = new File(upload + url);

            if (file.exists() == false) {
                pictrul.deleteById(p.getId());
                return false;
            }
            return true;

        }
        return false;
    }

    //根据md5查出对应图片的信息
    public Fileinfo getImg(String md5) {
        QueryWrapper<Fileinfo> query = new QueryWrapper<Fileinfo>();
        query.eq(true, "md5", md5);
        List<Fileinfo> list = pictrul.selectList(query);
        if (list.size() > 0) {
            Fileinfo p = list.get(0);
            return p;
        }
        return null;
    }

    //根据内容组件id获取组件布局
    public String getLayout(String id) {

        QueryWrapper<Component> query = new QueryWrapper<Component>();
        query.select("area");
        query.eq(true, "id", id);

        Component c = component.selectOne(query);

        if (c != null) {
            return c.getArea();
        }

        return null;
    }

    //根据主键删除fileinfo
    public void delImageById(int id) {
        pictrul.deleteById(id);
    }
}
